import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/ui/pages/splash_page.dart';
import 'package:dio/dio.dart';
import 'package:fluintl/fluintl.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'common/common.dart';
import 'common/global.dart';
import 'model/language_model.dart';
import 'provider/provider_manager.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'res/strings.dart';
import 'route/routes.dart';
import 'utils/sp_util.dart';
import 'view_model/my_app_view_model.dart';

void main() {
  Global.init(() {
    runApp(MultiProvider(
      providers: providers,
      child: MyApp(),
    ));
  });
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Locale _locale;

//  Color _themeColor = Colours.app_main;

  @override
  void initState() {
    super.initState();
    setLocalizedValues(localizedValues);
    init();
  }

  void init() {
    final router = new Router();
    Routes.configureRoutes(router);
    Application.router = router;
    _loadLocale();
  }

  void _loadLocale() {
    setState(() {
      LanguageModel model =
          SpUtil.getObj(Constant.keyLanguage, (v) => LanguageModel.fromJson(v));
      if (model != null) {
        _locale = new Locale(model.languageCode, model.countryCode);
      } else {
        _locale = null;
      }
//      String _colorKey = SpHelper.getThemeColor();
//      if (themeColorMap[_colorKey] != null)
//        _themeColor = themeColorMap[_colorKey];
    });
  }

  @override
  Widget build(BuildContext context) {
    MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(context);
    myAppViewModel.initView();
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "指针教育",
      theme: ThemeData.light().copyWith(
        brightness: Brightness.light,
        primaryColor: myAppViewModel.color,
        indicatorColor: myAppViewModel.color,
        accentColor: myAppViewModel.color,
      ),
      locale: _locale,
      localizationsDelegates: [
        // 本地化代理
        RefreshLocalizations.delegate, //下拉刷新
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        CustomLocalizations.delegate,
        const FallbackCupertinoLocalisationsDelegate(), //加入这个, 上面三个是我用来国际化的
      ],
      supportedLocales: CustomLocalizations.supportedLocales,
      home: SplashPage(),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class FallbackCupertinoLocalisationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const FallbackCupertinoLocalisationsDelegate();

  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      DefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}

class MyHemo extends StatefulWidget {
  @override
  _MyHemoState createState() => _MyHemoState();
}

class _MyHemoState extends State<MyHemo> {
  @override
  void initState() {
    super.initState();

    BaseOptions options = BaseOptions(
      baseUrl: 'http://127.0.0.1:8888',
      //连接服务器超时时间，单位是毫秒.
      connectTimeout: 1000 * 45,
      //响应流上前后两次接受到数据的间隔，单位为毫秒。
      receiveTimeout: 1000 * 45,
//      application/x-www-form-urlencoded格式编码,
      contentType: Headers.jsonContentType,
      headers: {
        "token": "1111111111111111",
      },
    );

    Dio dio = new Dio(options);
    //  是否开启请求日志
    dio.interceptors.add(LogInterceptor());
    dio.post("/productline").then((value) {
      print(value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text(
        "测试接口",
        style: TextStyle(color: Colors.blue),
      )),
      body: Center(),
    );
  }
}
